package cate.game.activity.taskactivity;

import cate.common.table.activity.taskactivity.row.TaskActivityBaseRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.mail.Mail;
import cate.game.role.task.TaskGroup;
import cate.game.role.task.TaskItem;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

public class RoleTaskActivityTask extends SubActivitySingle<RoleTaskActivity> {
    public int configGroup() {
        TaskActivityBaseRow baseRow = parent.getRow();
        if (baseRow == null) {
            return -1;
        } else {
            return baseRow.taskGroup;
        }
    }

    @Override
    public void onEnter(){
        excelCheck();
    }

    public void excelCheck() {
        TaskActivityBaseRow baseRow = parent.getRow();
        if(baseRow != null){
            if(baseRow.dayGroup){
                TaskGroup taskGroup = role.getTask().getGroup(funcId());
                if(taskGroup.ingList.isEmpty() && taskGroup.doneList.isEmpty()){
                    List<Integer> dayTaskIds = role.getGame().table.taskActivity.task.getDayTaskIds(configGroup(),
                            parent.getCurrentDay()
                    );
                    role.getTask().getGroup(funcId()).addItemListNoRepeat(dayTaskIds,false);
                }
            }else{
                List<Integer> taskIds = role.getGame().table.taskActivity.task.getTaskIds(configGroup());
                role.getTask().getGroup(funcId()).addItemListNoRepeat(taskIds,false);
            }
        }
    }

    public void beforeOver() {
        TaskActivityBaseRow baseRow = parent.getRow();
        if(baseRow == null || baseRow.mailId == 0){
            return;
        }
        sendDoneMail(baseRow);
    }

    public void clear() {
        parent.getRole().getTask().removeGroup(funcId());
    }

    @Override
    public void onDaySpan(boolean silence){
        TaskActivityBaseRow baseRow = parent.getRow();
        TaskGroup taskGroup = role.getTask().getGroup(funcId());
        if(baseRow != null && baseRow.dailyReset){
            //每日累充跨天发送未领取奖励
            sendDoneMail(baseRow);
            List<Integer> taskIds = role.getGame().table.taskActivity.task.getTaskIds(configGroup());
            taskGroup.removeItemList(taskIds);
            taskGroup.addItemListNoRepeat(taskIds,silence);
        }else if(baseRow != null && baseRow.dayGroup){
            sendDoneMail(baseRow);
            List<Integer> dayTaskIds = role.getGame().table.taskActivity.task.getDayTaskIds(configGroup(), parent.getCurrentDay());
            taskGroup.addItemListNoRepeat(dayTaskIds, silence);
        }
    }

    public void sendDoneMail(TaskActivityBaseRow baseRow){
        TaskGroup taskGroup = role.getTask().getGroup(funcId());
        MixRes reward = new MixRes();
        for(TaskItem taskItem : taskGroup.doneList){
            if(taskItem.done && !taskItem.rewardGot){
                reward.addList(new MixRes(taskItem.getTpl().rewardStr));
            }
        }
        if(!reward.isEmpty()){
            Mail mail = new MailBuilder()
                    .setReward(reward)
                    .setContentId(baseRow.mailId)
                    .setOperation(GDOperation.TASK_ACTIVITY_REWARD)
                    .setOperationSub(funcId())
                    .build();
            role.getGame().mail.send(role.getUid(), mail);
        }
        taskGroup.removeAllItems();
    }

    /**
     * 领取任务奖励
     *
     * @param taskId 任务ID
     * @return 领取结果 携带真实获得的资源
     */
    @JsonIgnore
    public GameResult<MixRes> getTaskRwd(int taskId) {
        //后续开启特惠礼包使用
        // TODO
        return new GameResult<MixRes>().success();
    }
}
